package com.sakura.thread.chapter1;

/**
 * Created by apple on 2018/12/10.
 */
public class WaitTask {
    private static volatile Object resourceA =new Object ();
    private static volatile Object resourceB =new Object() ;

    public static void main(String[] args) throws InterruptedException {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (resourceA){
                        System.out.println("threadA get resourceA Lock");

                        synchronized (resourceB){
                            System.out.println("threadA get resourceB Lock");

                            System.out.println("threadA release resourceA Lock");

                            resourceA.wait();

                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);

                    synchronized (resourceA){
                        System.out.println("threadB get resourceA Lock");

                        System.out.println("threadB try get resourceB Lock");

                        synchronized (resourceB){
                            System.out.println("threadB get resourceB Lock");

                            System.out.println("threadB release resourceA Lock");
                            resourceA.wait();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        threadA.start();
        threadB.start();

        threadA.join();
        threadB.join();

        System.out.println("over");
    }
}
